/**
 * Created by Administrator on 2017/3/17.
 */
import router from './index'
import Storage from 'util/lib/storage'
// start
const login = resolve => require(['views/client/login'], resolve)
const talk = resolve => require(['views/client/talk'], resolve)
const loan = resolve => require(['components/section/loan'], resolve)

// 浏览器参数
const location = window.location
// 浏览器参数
const params = (() => {
  let res = {}
  if (location.search) {
    const arr = location.search.substring(1).split('&')
    arr.forEach((item) => {
      let param = item.split('=')
      res[param[0]] = decodeURIComponent(param[1])
    })
  }
  return res
})()

const defaultMap = {}
// 抽取第三方跳转路由
const extraTPOSMap = () => {
  if (params && params.state && params.state.indexOf('/') === 0) {
    defaultMap.path = params.state
  }
}

export const redirectUrl = (() => {
  let routePath = '/client/login'
  if (!params.state) { // 非微信访问菜单 1、域名访问。 2、浏览者查看分享链接 - 参数带有【from & isappinstalled】
    if (location.hash && location.hash.length > 4) {
      return {path: location.hash ? location.hash.replace('#', '') : routePath, query: params}
    }
  }
  let queryParams = {}
  if(params.state && params.state.indexOf('/') === 0) { // 微信跳转
    let arr = params.state.split('?')
    if(arr[0]) {
      routePath = arr[0]
    }
    if(arr[1]) { // 附带查询参数
      let paramGroup = arr[1].split('SPA') // 解析
      if(paramGroup && paramGroup.length) {
        paramGroup.forEach( item => {
          queryParams[item.split('=')[0]] = item.split('=')[1]
        })
      }
    }
  }
  return {path: routePath, query: queryParams}
})
//路径统一配置前缀client name无前缀
export default [
  //地址为空时跳转home页面
  {
    path: '/',
    // redirect: '/client/login'
    redirect: redirectUrl
  },
  {
    path: '/client/login',
    name:'login',
    meta: {pageTitle: '仁心医联'},
    component: login,
    beforeEnter(to, from, next) {
      if(Storage.get("clientUserToken")) {

      }
      next();
    }
  },
  {
    path: '/client/loan',
    name:'loan',
    meta: {pageTitle: '实时记录'},
    component: loan,
    children: [
      {
        path: 'talk',
        name: 'talk',
        component: talk,
        meta: {pageTitle: '实时记录'}
      }
    ]
  }
]







